/*
 * $HeadURL:  $
 *
 * Copyright (c) 2007 Drutt Corporation, all rights reserved.
 *
 */
package homework.james.xiao.bean;

import homework.james.xiao.util.Util;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.LinkedList;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.io.xml.DomDriver;

/**
 * @author eyngxao
 * @version $Revision: $
 */
@XStreamAlias("menu") 
/**
 * 菜单，由若干个Dish组成
 */
public class Menu {

	/** Class revision */
	public static final String _REV_ID_ = "$Revision: $";
	private static final String DIR = "/src/homework/james/xiao/xml/";
	private static final String FILENAME = "menu.xml";
	
	private static Menu menu = null;
	private LinkedList<Dish> items = new LinkedList<Dish>();
	public LinkedList<Dish> getItems() {
		return items;
	}
	public void setItems(LinkedList<Dish> items) {
		this.items = items;
	}
	public void addDish(Dish d){
		items.add(d);
	}
	public static Menu getInstance() throws FileNotFoundException{
		if (menu==null){
			XStream xstream = new XStream(new DomDriver());
			Util.alias(xstream);
			menu = (Menu)xstream.fromXML(new FileInputStream(System.getProperty("user.dir")+ DIR+ FILENAME));
			return menu;
		}else{
			return menu;
		}
		
	}
	
	public void print(PrintWriter ps){
		String border = "=========================================";
		ps.println(border);
		ps.println("| NO. |\t  name\t\t|      price	|");
		ps.println(border);
		int number = 1;
		for(Dish dish:this.getItems()){
			ps.println("|  "+number +"  "+dish);
			number++;
		}
		ps.println(border);
	}

}
